package com.zui.webapp.support;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JsonDataHelper {
	
	
	
	/**
	 *
	    "Vegetables": {
	         "key" : 10,
             "defaultvalue" : 111,
	         "values" : {
                 "tomato": 110,
                 "potato": 111,
                 "asparagus": 112}},
        "Fruits": {
             "key" : 20,
             "defaultvalue" : 212,
             "values" : {
                 "apple": 210,
                 "orange": 211,
                 "kiwi": 212,
                 "melon": 213}}
	 * 
	 * @param firstMap firstKey:"10", firstValue:"Vegetables";
	 * @param secondMap firstKey:"10", secondMapValue: another map[key:"110",value:"tomato"]
	 * @return
	 */
	public static String getDoubleSelctJson(Map firstMap, Map<Object, Map> secondMap) {

		
		StringBuffer optionsBuf = new StringBuffer(); 
		Set keySet = firstMap.keySet();
		Iterator keySetIt = keySet.iterator();
		while (keySetIt.hasNext()) {
			Object key = keySetIt.next();
			optionsBuf.append("\"" + firstMap.get(key) + "\":" + "{");
			
			Map doubleMap = secondMap.get(key);
			Set doubleKeySet = doubleMap.keySet();
			
			Iterator doubleKeySetIt = doubleKeySet.iterator();
			optionsBuf.append("\"key\":" + "\"" + key + "\",");
			optionsBuf.append("\"values\": {" );
			
			while (doubleKeySetIt.hasNext()) {
				Object doubleKey = doubleKeySetIt.next();
				optionsBuf.append("\"" + doubleMap.get(doubleKey) + "\":\"" + doubleKey + "\",");
			}
			if (optionsBuf.lastIndexOf(",") == optionsBuf.length()-1)
				optionsBuf.deleteCharAt(optionsBuf.lastIndexOf(","));
			optionsBuf.append("}" );
			optionsBuf.append("}," );
		}
		if (!keySet.isEmpty())
			optionsBuf.deleteCharAt(optionsBuf.lastIndexOf(","));
		return optionsBuf.insert(0, "{").append("}").toString();
	}

}
